package Servlet;

import com.alibaba.fastjson.JSONObject;
import dao.MyConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueryPlaceServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String placeNumber = request.getParameter("placeNumber");
        JSONObject jsonObject = new JSONObject();
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        try {
            PreparedStatement ps = myCon.connection.prepareStatement("select * from place where PlaceNumber=?");
            ps.setString(1, placeNumber);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                jsonObject.put("PlacePosition", rs.getString(2));
                jsonObject.put("PlaceImage", rs.getString(3));
            }
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        if (jsonObject != null) {
            out.print(jsonObject.toJSONString());
        }
    }
}
